package com.gensdai.leliang.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;

import com.gensdai.leliang.R;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/6/8.
 */

public abstract class NetWorkBaseActivity extends RxAppCompatActivity {

    public Toolbar mToolbar;

    //旋转的进度框
    public ProgressDialog pd;
    //变换观察者对象的一些操作
    public Function<Observable, ObservableSource> composeFunction;
    //设置重试次数
    private final long RETRY_TIMES = 1;
    //是否显示进度框
    private boolean showLoading = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getLayoutResource() > 0) {
            setContentView(getLayoutResource());

            mToolbar = (Toolbar) findViewById(R.id.toolbar);
            if (mToolbar != null) {
                //把Toolbar设置成默认的actionbar
                setSupportActionBar(mToolbar);
                //Toolbar不为空的时候才执行这些方法
                if (isShowBackButton()) {
                    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                    getSupportActionBar().setHomeButtonEnabled(true);

                }

                if (isCustomTitle()) {

                } else {
                    //判断是否有定义标题 没有就默认
                    if (getTitleStr() != null && !TextUtils.isEmpty(getTitleStr())) {
                        getSupportActionBar().setTitle(getTitleStr());
                    } else {
                        getSupportActionBar().setTitle(getResources().getString(R.string.undefined_title));
                    }
                }

            }
        }

        //初始化
        init();
    }

    @Override
    public void setSupportActionBar(Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
    }

    protected abstract String getTitleStr();

    protected abstract boolean isShowBackButton();

    protected abstract boolean isCustomTitle();

    protected abstract int getLayoutResource();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home)
            onBackPressed();
        return super.onOptionsItemSelected(item);
    }


    //定义网络请求 activity
    private void init() {
        pd = new ProgressDialog(this);
        composeFunction = new Function<Observable, ObservableSource>() {
            @Override
            public ObservableSource apply(@NonNull Observable observable) throws Exception {
                return observable.retry(RETRY_TIMES)
                        .subscribeOn(Schedulers.io())
                        .doOnSubscribe(new Consumer<Disposable>() {
                            @Override
                            public void accept(@NonNull Disposable disposable) throws Exception {
                                //判断本地是否 网络连接畅通
                                if (showLoading) {
                                    if(pd != null && !pd.isShowing()){
                                        pd.show();
                                    }
                                }
                            }
                        });
            }
        };
    }

    public void setloadinFlag(boolean flag){
        showLoading =  flag;
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }
}
